package com.css.zfzx.fxpg.modules.percent.daynight.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.fxpg.common.utils.BeanUtil;
import com.css.zfzx.fxpg.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.fxpg.modules.percent.daynight.repository.FxDayNightPercentNativeRepository;
import com.css.zfzx.fxpg.modules.percent.daynight.repository.FxDayNightPercentRepository;
import com.css.zfzx.fxpg.modules.percent.daynight.repository.entity.FxDayNightPercentEntity;
import com.css.zfzx.fxpg.modules.percent.daynight.service.FxDayNightPercentService;
import com.css.zfzx.fxpg.modules.percent.daynight.viewobjects.FxDayNightPercentQueryParams;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.fxpg.modules.percent.daynight.viewobjects.FxDayNightPercentVO;
import com.css.zfzx.fxpg.modules.vulnerability.province.repository.entity.FxVulnerabilityProvinceEntity;
import com.css.zfzx.fxpg.modules.vulnerability.province.viewobjects.FxVulnerabilityProvinceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-12
 */
@Service
public class FxDayNightPercentServiceImpl implements FxDayNightPercentService {

    @Autowired
    private FxDayNightPercentRepository fxDayNightPercentRepository;
    @Autowired
    private FxDayNightPercentNativeRepository fxDayNightPercentNativeRepository;

    @Override
    public JSONObject queryFxDayNightPercents(FxDayNightPercentQueryParams queryParams, int curPage, int pageSize) {
        Page<FxDayNightPercentEntity> fxDayNightPercentPage = this.fxDayNightPercentNativeRepository.queryFxDayNightPercents(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxDayNightPercentPage);
        return jsonObject;
    }


    @Override
    public FxDayNightPercentEntity getFxDayNightPercent(String id) {
        FxDayNightPercentEntity fxDayNightPercent = this.fxDayNightPercentRepository.findById(id).orElse(null);
        return fxDayNightPercent;
    }

    @Override
    public boolean isRepeat(String houseType) {
        FxDayNightPercentEntity fxDayNightPercentEntity = fxDayNightPercentRepository.findDistinctByHouseType(houseType);
        if (fxDayNightPercentEntity != null) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxDayNightPercentEntity saveFxDayNightPercent(FxDayNightPercentEntity fxDayNightPercent) {
        String uuid = UUIDGenerator.getUUID();
        fxDayNightPercent.setId(uuid);
        fxDayNightPercent.setCreateUser(PlatformSessionUtils.getUserId());
        fxDayNightPercent.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.fxDayNightPercentRepository.save(fxDayNightPercent);
        return fxDayNightPercent;
    }

    @Override
    public void coverFxDayNightPercent(FxDayNightPercentEntity fxDayNightPercent) {
        FxDayNightPercentEntity old = fxDayNightPercentRepository.findDistinctByHouseType(fxDayNightPercent.getHouseType());
        BeanUtil.copyPropertiesIgnoreEmpty(fxDayNightPercent, old);
        this.updateFxDayNightPercent(old);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxDayNightPercentEntity updateFxDayNightPercent(FxDayNightPercentEntity fxDayNightPercent) {
        fxDayNightPercent.setUpdateUser(PlatformSessionUtils.getUserId());
        fxDayNightPercent.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = fxDayNightPercent.getId();
        // 尽量别用getOne()
        FxDayNightPercentEntity one = fxDayNightPercentRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(fxDayNightPercent, one);
        this.fxDayNightPercentRepository.save(one);
        return fxDayNightPercent;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFxDayNightPercents(List<String> ids) {
        List<FxDayNightPercentEntity> fxDayNightPercentList = this.fxDayNightPercentRepository.findAllById(ids);
        if (!PlatformObjectUtils.isEmpty(fxDayNightPercentList) && fxDayNightPercentList.size() > 0) {
            for (FxDayNightPercentEntity fxDayNightPercent : fxDayNightPercentList) {
                this.fxDayNightPercentRepository.delete(fxDayNightPercent);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public String exportExcel(FxDayNightPercentQueryParams queryParams, HttpServletResponse response) {
        try {
            List<FxDayNightPercentEntity> entities = fxDayNightPercentNativeRepository.listFxDayNightPercents(queryParams);
            // 深拷贝
            ArrayList<FxDayNightPercentVO> vos = new ArrayList<>();
            for (FxDayNightPercentEntity entity : entities) {
                FxDayNightPercentVO vo = new FxDayNightPercentVO();
                BeanUtils.copyProperties(entity, vo);
                vos.add(vo);
            }
            // 导出
            EasyPoiExcelUtil.exportExcel(vos, "人员昼夜百分比", "人员昼夜百分比", FxDayNightPercentVO.class, "人员昼夜百分比.xlsx", response);
            return "导出成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}
